<template>
  <div class="flow-node">
  <!-- 非网关节点 -->
  <div class="node-wrap" v-if="[NODE.START, NODE.APPROVE, NODE.COPY, NODE.TRANSACT, NODE.TRIGGER, NODE.DELAY].includes(nodeConfig.type)">
    <div class="node-wrap-box" :class="{ 'start-node': nodeConfig.type == NODE.START }">
      <div class="title" :style="{ background: nodeBgColor }">
        <!-- 开始节点 -->
        <span v-if="nodeConfig.type == NODE.START">{{ nodeConfig.name }}</span>
        <!-- 任务、办理、抄送节点 -->
        <template v-else>
          <!-- <span class="iconfont-approval-admin">{{ nodeConfig.type == 1 ? "&#xe658" : "&#xe656" }}</span> -->
          <input
            v-if="isNodeNameEdit"
            type="text"
            class="editable-title-input"
            v-model="nodeConfig.name"
            :maxlength="16"
            :placeholder="nodeDefaultName" />
          <template v-else>
            <span class="editable-title">
              <span>{{ nodeConfig.name }}</span>
            </span>
          </template>
        </template>
      </div>
      <div class="content">
        <!-- 发起人节点 -->
        <a-tooltip v-if="nodeConfig.type == NODE.START" :content="showNodeContent" mini content-class="node-content-tooltip">
          <span class="text">{{ nodeDefaultName }}：{{ showNodeContent }}</span>
        </a-tooltip>
        <!-- 审核人、抄送人、办理人节点 -->
        <template v-else-if="[NODE.APPROVE, NODE.COPY, NODE.TRANSACT, NODE.TRIGGER].includes(nodeConfig.type)">
			<span class="placeholder" v-if="!showNodeContent">请选择{{ nodeDefaultName }}</span>
			<template v-else>
				<span class="text">{{ nodeDefaultName }}：</span>
				<template v-for="(item, index) in showNodeContent">
					<template v-if="item.type==='text'">
						<ex-label :key="item.data" :code="item.type" :value="item.data"></ex-label>
					</template>
					<template v-else>
						<template v-for="(ite, inde) in item.data" :key="ite">
							<ex-label  :code="item.type" :value="ite"></ex-label>
						</template>
					</template>
				</template>
			</template>
        </template>
        <template v-else-if="[ NODE.DELAY].includes(nodeConfig.type)">
          <a-tooltip v-if="showNodeContent" mini :content="showNodeContent" content-class="node-content-tooltip">
            <span class="text">{{ showNodeContent }} </span>
          </a-tooltip>
        </template>
      </div>
    </div>
    <FlowAddNode v-model:childNodeP="nodeConfig.childNode" />
  </div>

  <!-- 网关节点 -->
  <div class="branch-wrap" style=" margin-top: -2%;" v-if="nodeConfig.type == NODE.EXCLUSIVE_GATEWANY">
    <div class="branch-box-wrap">
      <div class="branch-box">
        <!-- 网关分支节点 -->
        <div class="col-box" v-for="(item, index) in nodeConfig.conditionNodes" :key="index">
          <div class="condition-node">
            <div class="condition-node-box">
              <!-- 默认分支条件 -->
              <div class="auto-judge default-branch-node" v-if="isDefaultBranchNode(index)">
                <div class="title-wrapper">
                  <span class="editable-title">默认条件</span>
                  <span class="priority-title">优先级{{ item.priorityLevel }}</span>
                </div>
                <div class="content-wrapper">
                  <div class="content">未满足时其他条件时，将进入默认流程</div>
                </div>
              </div>
              <!-- 其他分支条件 -->
              <div v-else class="auto-judge">
                <div class="title-wrapper">
                  <input
                    v-if="nodeNameInputList[index]"
                    type="text"
                    class="editable-title-input"
                    :maxlength="16"
                    v-model="item.name" />
                  <template v-else>
                    <span class="editable-title"> {{ item.name }} </span>
                    <span class="priority-title">优先级{{ item.priorityLevel }}</span>
                  </template>
                </div>
                <div class="content-wrapper">
                  <div class="sort-left" v-if="index != 0">
                    <icon-left />
                  </div>
                  <div class="content">
                    <span v-if="(getGatewayBranch(nodeConfig, index).conditionGroups || []).length == 0" class="placeholder">请设置条件</span>
                    <!-- 卡片上显示分支条件 -->
                    <a-tooltip v-else mini :content="showConditionContent(nodeConfig, index)" content-class="node-content-tooltip">
                      <span class="text">{{ showConditionContent(nodeConfig, index) }}</span>
                    </a-tooltip>
                  </div>
                </div>
              </div>
              <FlowAddNode v-model:childNodeP="item.childNode" />
            </div>
          </div>
          <FlowNodeWrap v-if="item.childNode" v-model:nodeConfig="item.childNode" v-model:flow-widgets="props.flowWidgets"/>
          <template v-if="index == 0">
            <div class="top-left-cover-line"></div>
            <div class="bottom-left-cover-line"></div>
          </template>
          <template v-if="index == nodeConfig.conditionNodes.length - 1">
            <div class="top-right-cover-line"></div>
            <div class="bottom-right-cover-line"></div>
          </template>
        </div>
      </div>
      <FlowAddNode v-model:childNodeP="nodeConfig.childNode" />
    </div>
  </div>

  <!-- 分支汇合节点 -->
  <FlowNodeWrap v-if="nodeConfig.childNode" v-model:nodeConfig="nodeConfig.childNode" v-model:flow-widgets="props.flowWidgets" />
  </div>
</template>

<script setup>
import { computed, onMounted, reactive, ref } from "vue";
import { showExpNodeContent } from "@/components/flowable/flow/common/FormExp";
import FlowAddNode from "@/components/flowable/flowModel/flowAddNode.vue";
import { NODE } from "@/components/flowable/flow/common/FlowConstant";
import {  IconLeft } from "@arco-design/web-vue/es/icon";
import { getNameById, getRoleNameById, getUserNameById } from "@/api/flowable/SysOptionApi";
import ExLabel from "@/components/exLabel/index.vue";

// 属性
let props = defineProps({
	nodeConfig: { type: Object, default: () => ({}) },
	flowWidgets: { type: Object, default: () => ({}) },
	flowPermission: { type: Object, default: () => [] },
});

// 节点基本信息
let nodeSettings = reactive({});
nodeSettings[NODE.START] = { placeholder: "发起人", bgColor: "#a9b4cd" };
nodeSettings[NODE.APPROVE] = { placeholder: "审核人", bgColor: "#ff943e" };
nodeSettings[NODE.COPY] = { placeholder: "抄送人", bgColor: "#3296fa" };
nodeSettings[NODE.TRANSACT] = { placeholder: "办理人", bgColor: "#926bd5" };
nodeSettings[NODE.TRIGGER] = { placeholder: "触发器", bgColor: "#15BC83" };
nodeSettings[NODE.DELAY] = { placeholder: "延迟处理", bgColor: "#f25643" };
let nodeDefaultName = computed(() => nodeSettings[props.nodeConfig.type].placeholder);
let nodeBgColor = computed(() => nodeSettings[props.nodeConfig.type].bgColor);
// nodeType 0发起人 1审批 2抄送 3条件 4路由
// 节点卡片显示的内容
let showNodeContent = computed(() => {
  let nodeType = props.nodeConfig.type;
  if (nodeType == NODE.START) {
    // 开始节点
    let { type, flowInitiators } = props.flowPermission;
    if (type == 0) return "制单人可提交";
    else if (type == 2) return "均不可提交";
    else return (flowInitiators || []).map((i) => getNameById(i.id)).join(", ");
  } else if (nodeType == NODE.APPROVE) {
    // 审批人节点
    let { assignees, approvalType } = props.nodeConfig;
    if (approvalType == 1) return "自动通过";
    else if (approvalType == 2) return "自动拒绝";
    else {
		return assignees
			.map((assignee) => {
				let { assigneeType, layerType, layer, roles, assignees } = assignee;
				if (assigneeType == 0) return {type:"text",data:"发起人本人"};
				else if (assigneeType == 1) {
					if (layerType == 0) return {type:"text",data:`直属${layer != 0 ? layer + "级" : ""}上级`};
					else return `最高上级减${layer != 0 ? layer + "级" : ""}`;
				} else if (assigneeType == 2) {
					if (layerType == 0) return {type:"text",data:`直属${layer != 0 ? layer + "级" : ""}部门负责人`};
					else return {type:"text",data:`最高部门负责人减${layer != 0 ? layer + "级" : ""}`};
				} else if (assigneeType == 3) {
					if(roles){
						return {type:"SysRole",data:roles}
					}else{
						return {type:"SysRole",data:[]}
					}
				} else if (assigneeType == 4) {
					if(assignees){
						return {type:"SysUser",data:assignees};
					}else{
						return {type:"SysUser",data:[]};
					}
				} else if (assigneeType == 5) {
					return {type:"text",data:"连续多级上级"};
				} else if (assigneeType == 6) {
					return {type:"text",data:"连续多级部门负责人"};
				} else if (assigneeType == 7) {
					return {type:"text",data:"发起人自选"};
				}
			});
    }
  } else if (nodeType == NODE.COPY) {
	  // 抄送人节点
	  let { ccs } = props.nodeConfig;
	  return ccs
		  .map((cc) => {
			  let { ccType, layerType, layer, roles, assignees } = cc;
			  if (ccType == 0) return {type:"text",data:"发起人本人"};
			  else if (ccType == 1) {
				  if (layerType == 0) return {type:"text",data:`直属${layer != 0 ? layer + "级" : ""}上级`};
				  else return {type:"text",data:`最高上级减${layer != 0 ? layer + "级" : ""}`};
			  } else if (ccType == 2) {
				  if (layerType == 0) return {type:"text",data:`直属${layer != 0 ? layer + "级" : ""}部门负责人`};
				  else return {type:"text",data:`最高部门负责人减${layer != 0 ? layer + "级" : ""}`};
			  } else if (ccType == 3) {
				  if(roles){
					  return {type:"SysRole",data:roles}
				  }else{
					  return {type:"SysRole",data:[]}
				  }
			  } else if (ccType == 4) {
				  if(assignees){
					  return {type:"SysUser",data:assignees};
				  }else{
					  return {type:"SysUser",data:[]};
				  }
			  }
		  });
  } else if (nodeType == NODE.TRANSACT) {
	  // 办理人节点
	  let { transactors } = props.nodeConfig;
	  return transactors
		  .map((transactor) => {
			  let { transactorType, layerType, layer, roles, assignees } = transactor;
			  if (transactorType == 0) return {type:"text",data:"发起人本人"};
			  else if (transactorType == 1) {
				  if (layerType == 0) return {type:"text",data:`直属${layer != 0 ? layer + "级" : ""}上级`};
				  else return {type:"text",data:`最高上级减${layer != 0 ? layer + "级" : ""}`};
			  } else if (transactorType == 2) {
				  if (layerType == 0) return {type:"text",data:`直属${layer != 0 ? layer + "级" : ""}部门负责人`};
				  else return {type:"text",data:`最高部门负责人减${layer != 0 ? layer + "级" : ""}`};
			  } else if (ccType == 3) {
				  if(roles){
					  return {type:"SysRole",data:roles}
				  }else{
					  return {type:"SysRole",data:[]}
				  }
			  } else if (ccType == 4) {
				  if(assignees){
					  return {type:"SysUser",data:assignees};
				  }else{
					  return {type:"SysUser",data:[]};
				  }
			  } else if (transactorType == 7) {
				  return {type:"text",data:"发起人自选"};
			  }
		  });
  } else if(nodeType == NODE.DELAY){
    let delayCommot = props.nodeConfig.DelayConfig;
    if(delayCommot.type == "FIXED"){
      let dayConmt = 0;
      if(delayCommot.time){
        dayConmt = delayCommot.time;
      }
      return '等待'+ dayConmt + getName(delayCommot.unit);
    }else if(delayCommot.type == "AUTO"){
      return '至当天'+ delayCommot.dateTime;
    }
  }
  return null;
});

const getName = (unit) =>{
      switch (unit){
        case 'D': return '天';
        case 'H': return '小时';
        case 'M': return '分钟';
        default: return '天';
      }
    }
// 分支卡片显示的内容
let showConditionContent = (nodeConfig, index) => {
  let branchNode = nodeConfig.conditionNodes[index];
  //console.log("条件分支：", branchNode);
  return showExpNodeContent(branchNode, props.flowWidgets);
};
let getGatewayBranch = (gateway, index) => {
  return gateway.conditionNodes[index];
};
// 是否为默认分支节点
const isDefaultBranchNode = (idx) => {
  return props.nodeConfig.conditionNodes.length == idx + 1;
};

let nodeNameInputList = ref([]);
let isNodeNameEdit = ref(false);

onMounted(() => {
});
</script>

<style lang="less">
@node-padding: 12px;
@node-width: 210px;
@node-heigth: 78px;
@node-border-radius: 8px;
@canvas-bg: #f5f5f7;
.node-wrap,
.branch-wrap {
  display: inline-flex;
  width: 100%;

  // 头部输入框
  .editable-title-input {
    width: 100%;
    height: 18px;
    margin: 0;
    padding-left: 4px;
    font-size: 12px;
    line-height: 18px;
    color: rgba(0, 0, 0, 0.65);
    background-color: #fff;
    border: 1px solid #d9d9d9;
    border-radius: 4px;
    transition: all 0.3s;

    &::placeholder {
      color: #bfbfbf;
    }

    &:focus {
      outline: 0;
      border-color: #40a9ff;
      box-shadow: 0 0 0 1px rgba(24, 144, 255, 0.2);
    }

    &:hover {
      text-decoration: none;
      border-color: #40a9ff;
    }
  }
}

// 节点卡片、分支卡片样式
.node-wrap .node-wrap-box,
.branch-wrap .auto-judge {
  &::after {
    pointer-events: none;
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 2;
    border-radius: @node-border-radius;
    border: 1px solid transparent;
    transition: all 0.1s cubic-bezier(0.645, 0.045, 0.355, 1);
    box-shadow: 0 2px 5px #0000001a;
  }

  &:hover {
    &::after {
      // border: 1px solid #3296fa;
      // box-shadow: 0 0 6px #3296fa4d;
      box-shadow: 0 13px 27px 0 #0000001a;
    }
  }

  .close {
    display: none;

    .arco-link-icon {
      color: #8f959e;
      margin-right: 0;
    }
  }
}

// 节点样式
.node-wrap {
  flex-direction: column;
  -webkit-box-pack: start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  padding: 0 50px;
  position: relative;

  .start-node {
    &::before {
      content: none !important;
    }
  }

  .node-wrap-box {
    position: relative;
    width: @node-width;
    min-height: @node-heigth;
    background: #fff;
    border-radius: @node-border-radius;
    cursor: pointer;
    display: flex;
    flex-direction: column;

    &::before {
      content: "";
      position: absolute;
      top: -8px;
      left: 50%;
      -webkit-transform: translateX(-50%);
      transform: translate(-50%);
      width: 0;
      height: 4px;
      border-style: solid;
      border-width: 8px 6px 4px;
      border-color: #cacaca transparent transparent;
      background: @canvas-bg;
    }

    &:hover {
      .close {
        display: inherit;
      }
    }

    .title {
      border-radius: @node-border-radius @node-border-radius 0 0;
      height: 28px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 12px;
      color: #fff;
      padding: 0 @node-padding;

      &:hover {
        .editable-title span {
          border-bottom: 1px dashed #fff;
        }
      }

      .iconfont {
        font-size: 12px;
        margin-right: 5px;
      }

      .editable-title {
        flex: 1;
      }
    }

    .content {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 5px @node-padding;

      .text {
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: normal;
        word-wrap: break-word;
        word-break: break-all;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }

      .placeholder {
        color: #a1a5ad;
      }
    }
  }
}

// 分支
.branch-wrap {
  .branch-box-wrap {
    display: flex;
    -webkit-box-orient: vertical;
    flex-direction: column;
    flex-wrap: wrap;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    min-height: 270px;
    width: 100%;
    -ms-flex-negative: 0;
    flex-shrink: 0;

    .branch-box {
      display: flex;
      overflow: visible;
      min-height: 180px;
      height: auto;
      border-bottom: 2px solid #ccc;
      border-top: 2px solid #ccc;
      position: relative;
      margin-top: 15px;

      .col-box {
        background: @canvas-bg;
        display: inline-flex;
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        flex-direction: column;
        -webkit-box-align: center;
        align-items: center;
        position: relative;

        &:before {
          content: "";
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          z-index: 0;
          margin: auto;
          width: 2px;
          height: 100%;
          background-color: #cacaca;
        }

        .condition-node,
        .condition-node-box {
          display: inline-flex;
          -webkit-box-orient: vertical;
          -webkit-box-direction: normal;
          flex-direction: column;
          -webkit-box-flex: 1;
        }

        .condition-node {
          /* min-height: 220px */

          .condition-node-box {
            padding-top: 30px;
            padding-right: 50px;
            padding-left: 50px;
            -webkit-box-pack: center;
            justify-content: center;
            -webkit-box-align: center;
            align-items: center;
            flex-grow: 1;
            position: relative;

            &:before {
              content: "";
              position: absolute;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
              margin: auto;
              width: 2px;
              height: 100%;
              background-color: #cacaca;
            }
          }
        }
      }

      // 分页卡片样式
      .auto-judge {
        position: relative;
        width: @node-width;
        min-height: @node-heigth;
        background: #fff;
        border-radius: @node-border-radius;
        cursor: pointer;
        display: flex;
        flex-direction: column;

        &:hover {
          .priority-title {
            display: none;
          }

          .close {
            display: inherit;
          }
        }

        .title-wrapper {
          position: relative;
          font-size: 12px;
          color: #2eb795;
          height: 28px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 0 @node-padding;
          border-radius: @node-border-radius @node-border-radius 0 0;
          border-bottom: 1px solid #f6f6f7;

          &:hover {
            .editable-title {
              border-bottom: 1px dashed #2eb795;
            }
          }
        }

        .content-wrapper {
          display: flex;
          flex: 1;
          align-items: center;

          .sort-left,
          .sort-right {
            height: 100%;
            display: flex;
            align-items: center;
            padding: 0 2px;

            &:hover {
              background: #efefef;
            }
          }

          .content {
            flex: 1;
            padding: 0 @node-padding;
            min-height: 100%;
            display: flex;
            align-items: center;

            .text {
              width: 100%;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: normal;
              word-wrap: break-word;
              word-break: break-all;
              display: -webkit-box;
              -webkit-line-clamp: 2;
              -webkit-box-orient: vertical;
            }

            .placeholder {
              color: #a1a5ad;
            }
          }
        }
      }

      .default-branch-node {
        color: #646a73 !important;

        .title-wrapper {
          color: #646a73 !important;
        }
      }

      .add-branch {
        border: none;
        outline: none;
        user-select: none;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 12px;
        padding: 0 10px;
        height: 30px;
        border-radius: 15px;
        color: #3296fa;
        background: #fff;
        cursor: pointer;
        box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
        position: absolute;
        top: -16px;
        left: 50%;
        transform: translateX(-50%);
        z-index: 1;
        transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);

        &::after {
          content: "";
          position: absolute;
          top: 0;
          bottom: 0;
          left: 0;
          right: 0;
          border-radius: 15px;
        }

        &:hover {
          // transform: translate(-50%);
          // box-shadow: 0 8px 16px #0000001a;

          &::after {
            border: 1px solid #3296fa;
            box-shadow: 0 0 6px #3296fa4d;
          }
        }
      }

      .top-left-cover-line {
        left: -1px;
      }

      .top-right-cover-line {
        right: -1px;
      }

      .bottom-left-cover-line {
        left: -1px;
      }

      .bottom-right-cover-line {
        right: -1px;
      }

      .top-left-cover-line,
      .top-right-cover-line {
        position: absolute;
        height: 8px;
        width: 50%;
        background-color: @canvas-bg;
        top: -4px;
      }

      .bottom-left-cover-line,
      .bottom-right-cover-line {
        position: absolute;
        height: 8px;
        width: 50%;
        background-color: @canvas-bg;
        bottom: -4px;
      }
    }
  }
}

.node-content-tooltip {
  font-size: 12px;
  border-radius: var(--border-radius-medium);
}
</style>
